#include "RankLayer.h"

bool RankLayer::init()
{
	if ( !CCLayer::init() )
    {
        return false;
    }

	winSize = CCDirector::sharedDirector()->getWinSize();



	CCMenu* menu = CCMenu::create();
	CCLabelTTF* label = CCLabelTTF::create("Back", "맑은 고딕", 40);
	CCMenuItemLabel* menuItem = CCMenuItemLabel::create(label, this, menu_selector(GameUiLayer::btnBack));
	menuItem->setPosition(ccp(winSize.width * 0.9f, winSize.height * 0.1f));
	menu->addChild(menuItem, 10);
    
    label = CCLabelTTF::create("doPost", "맑은 고딕", 40);
	menuItem = CCMenuItemLabel::create(label, this, menu_selector(RankLayer::doPost));
	menuItem->setPosition(ccp(winSize.width * 0.5f, winSize.height * 0.6f));
	menu->addChild(menuItem, 10);
    
    label = CCLabelTTF::create("setData", "맑은 고딕", 40);
	menuItem = CCMenuItemLabel::create(label, this, menu_selector(RankLayer::setData));
	menuItem->setPosition(ccp(winSize.width * 0.7f, winSize.height * 0.5f));
	menu->addChild(menuItem, 10);
    
    label = CCLabelTTF::create("getData", "맑은 고딕", 40);
	menuItem = CCMenuItemLabel::create(label, this, menu_selector(RankLayer::getData));
	menuItem->setPosition(ccp(winSize.width * 0.5f, winSize.height * 0.4f));
	menu->addChild(menuItem, 10);
    
    label = CCLabelTTF::create("clearData", "맑은 고딕", 40);
	menuItem = CCMenuItemLabel::create(label, this, menu_selector(RankLayer::clearData));
	menuItem->setPosition(ccp(winSize.width * 0.5f, winSize.height * 0.3f));
	menu->addChild(menuItem, 10);
    

	menu->setPosition(CCPointZero);
	this->addChild(menu, 10);


    edtBox = CCEditBox::create(CCSizeMake(400, 60), CCScale9Sprite::create("rec.png"));
    edtBox->setPosition(ccp(winSize.width * 0.3f, winSize.height * 0.5f));
    edtBox->setFontColor(ccBLACK);
    edtBox->setPlaceHolder("data");
    edtBox->setMaxLength(14);
    edtBox->setReturnType(kKeyboardReturnTypeDone);
    edtBox->setInputMode(kEditBoxInputModeSingleLine);
    this->addChild(edtBox);

/*
	CCHttpRequest* request = new CCHttpRequest();
	request->setUrl("http://115.71.238.29:8080/SegmentWeb/ranking/");
	request->setRequestType(CCHttpRequest::kHttpPost);


	std::vector<std::string> headers;
	headers.push_back("Content-Type: application/json; charset=utf-8");
	request->setHeaders(headers);
	request->setResponseCallback(this, callfuncND_selector(RankLayer::onHttpRequestCompleted));


	request->setTag("TEST");
	CCHttpClient::getInstance()->send(request);
	request->release();
*/
    
    
	    
	return true;
}

void RankLayer::update(float dt)
{

}


void RankLayer::doPost(CCObject* pSender)
{
	std::string userCode = CCUserDefault::sharedUserDefault()->getStringForKey("userCode");
	//http://192.168.1.105:8080/ranking/ranking/?userCode=%s
	CCString* url = CCString::createWithFormat("http://115.71.238.29:8080/SegmentWeb/ranking/?userCode=%s", userCode.c_str());

	CCMessageBox(url->getCString(),"url");

	CCHttpRequest* request = new CCHttpRequest();
	//request->setUrl("http://115.71.238.29:8080/SegmentWeb/ranking/");
	//CCMessageBox(sUrl->getCString(),"url");
	request->setUrl(url->getCString());
	//request->setRequestType(CCHttpRequest::kHttpPost);
	request->setRequestType(CCHttpRequest::kHttpGet);

	std::vector<std::string> headers;
	headers.push_back("Content-Type: application/json; charset=utf-8");
	request->setHeaders(headers);
	request->setResponseCallback(this, callfuncND_selector(RankLayer::onHttpRequestCompleted));

	//const char* postData = "sUserCode=20130720000001";
	//const char* postData = "{\"sUserCode\":\"20130720000001\"}";
	//request->setRequestData(postData, strlen(postData));

	request->setTag("ranking");
	CCHttpClient::getInstance()->send(request);
	request->release();

}

void RankLayer::onHttpRequestCompleted(CCNode *sender, void *data)
{
	CCHttpResponse *response = (CCHttpResponse*)data;


	if(!response)
	{
		return;
	}

	//int statusCode = response->getResponseCode();

	if(!response->isSucceed())
	{
		CCMessageBox(response->getErrorBuffer(),"Response Error");
		return;
	}
	
	// µ•¿Ã≈Õ ¿˙¿Â
	std::vector<char> *buffer = response->getResponseData();
	char* rcvData = (char*)malloc(buffer->size()+1);
	std::string s2(buffer->begin(), buffer->end());

	strcpy(rcvData, s2.c_str());

	
	std::string clearData((const char*)rcvData);
	size_t pos = clearData.rfind("}");
	clearData = clearData.substr(0, pos+1);
	
	if(clearData == "")
	{
		CCMessageBox("데이터가 없습니다.", "Error");
		return;
	}

	//CCMessageBox(clearData.c_str(), "clearData");



	Document document;

	if(document.Parse<0>(clearData.c_str()).HasParseError())
	{
		CCMessageBox(document.GetParseError(), "Parse Error");
	}
	else
	{
		assert(document.IsObject());
		assert(document.HasMember("nickname"));
		assert(document["nickname"].IsString());
		
		CCMessageBox(document["nickname"].GetString(), "nickname");

		//const Value& a = document;
		//assert(a.IsArray());
		//a[SizeType(0)].GetString();
		////a.

		//CCMessageBox(a.GetString(), "333");

		//std::string str_00 = document["addr"].GetString();
		//std::string str_00 = document["JSON"].GetString();
		
		//CCMessageBox(a[0].Getsgring(), "json");

		//CCMessageBox("333", "333");

        
	}

}



void RankLayer::setData(CCObject* pSender)
{
    if(edtBox->getText() == NULL)
        return;
    CCUserDefault::sharedUserDefault()->setStringForKey("userCode", edtBox->getText());
}

void RankLayer::getData(CCObject* pSender)
{
    std::string ret = CCUserDefault::sharedUserDefault()->getStringForKey("userCode");
    CCMessageBox(ret.c_str(), "userCode");
    
}

void RankLayer::clearData(CCObject* pSender)
{
    CCUserDefault::sharedUserDefault()->setStringForKey("userCode", "");
}